package club.itstudio.IContainer.service;

import club.itstudio.IContainer.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Service
public class UserService {
    private JdbcTemplate jdbcTemplate;
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    private UserMapper userMapper = new UserMapper();

    @Autowired
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    // insert user without id
    public boolean insert(User user) {
        String sql = "insert into user(username, password, user_type, last_login_time, created_time) " +
                "values(?, ?, ?, ?, ?)";
        try {
            jdbcTemplate.update(sql, user.userName, user.password, user.userType, user.lastLoginTime, user.createdTime);
        }catch (DataAccessException e) {
            logger.error("error insert new user", e);
            return false;
        }
        return true;
    }

    // return user or null
    public User getByUserName(String userName) {
        String sql = "select * from user where username=?";
        User user = null;
        try {
            user = jdbcTemplate.queryForObject(sql, userMapper, userName);
        }catch (DataAccessException e) {
             logger.error("error get user by username:" + userName);
        }
        return user;
    }

    // update last login time by userName
    public boolean updateLastLoginTime(User user) {
        String sql = "update user set last_login_time=? where username=?";
        try {
            jdbcTemplate.update(sql, user.lastLoginTime, user.userName);
        }catch (DataAccessException e) {
            return false;
        }
        return true;
    }

    // update password by userName
    public boolean updatePassword(User user) {
        String sql = "update user set password=? where username=?";
        try {
            jdbcTemplate.update(sql, user.password, user.userName);
        }catch (DataAccessException e) {
            return false;
        }
        return true;
    }

    public List<User> getUserListByUserType(int currentPage, int pageSize, int userType) {
        String sql = "select * from user where user_type=? order by created_time DESC limit ?,?";
        return jdbcTemplate.query(sql, userMapper, userType, (currentPage - 1) * pageSize, pageSize);
    }

    public int getUserNumByUserType(int userType) {
        String sql = "select count(*) from user where user_type=?";
        return jdbcTemplate.queryForObject(sql, Integer.class, userType);
    }

}

class UserMapper implements RowMapper<User> {
    @Override
    public User mapRow(ResultSet resultSet, int i) throws SQLException {
        User user = new User();
        user.id = resultSet.getInt("id");
        user.userName = resultSet.getString("username");
        user.password = resultSet.getString("password");
        user.userType = resultSet.getInt("user_type");
        user.lastLoginTime = resultSet.getLong("last_login_time");
        user.createdTime = resultSet.getLong("created_time");
        return user;
    }
}